package com.technology.education.ui.common.splash

import android.os.Bundle
import android.os.PersistableBundle
import com.technology.education.data.AppData
import com.technology.education.data.SpKeys
import com.technology.education.databinding.ActivitySplashBinding
import com.technology.education.ext.getSpString
import com.technology.education.ext.isLogin
import com.technology.education.ext.saveData
import com.technology.education.ext.showLog
import com.technology.education.ui.common.role_selector.RoleSelectorActivity
import com.technology.education.ui.market.MainMarketActivity
import com.technology.education.ui.student.MainStudentActivity
import com.technology.education.ui.teacher.MainTeacherActivity
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.util.startActivity
import java.lang.Exception

/**
 *@author:create by BZY
 *Date: 2022/2/24 09
 */
class ActivitySplash : BaseVmDbActivity<BaseViewModel, ActivitySplashBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!isTaskRoot) {
            finish()
            return
        }
    }

    private fun startCount() {
        try {
            mDatabind.ivSplash.postDelayed({
                if (isLogin()) {
                    AppData.TOKEN = getSpString(SpKeys.TOKEN)
                    AppData.MEMBER_TYPE = getSpString(SpKeys.MEMBER_TYPE)
                    AppData.USER_ID = getSpString(SpKeys.USER_ID)
                    when (AppData.MEMBER_TYPE) {
                        "0" -> {
                            startActivity(MainStudentActivity::class)
                        }
                        "1" -> {
                            startActivity(MainTeacherActivity::class)
                        }
                        "2" -> {
                            startActivity(MainMarketActivity::class)
                        }
                    }

                } else {
                    startActivity(MainStudentActivity::class)
                }
                finish()
            }, 1000)
        } catch (e: Exception) {
            showLog(e.toString())
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        startCount()
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    override fun createObserver() {
    }
}